package com.datastructure

/**
 * @description:
 *
 * List is immutable and lists have a recursive structure
 * @author: marshal
 * @date: 2021-08-10 11:33 AM
 */
object ListTest extends App {

  // 1. declare
  val l = List(1, 2, 3)
  val l2 = List.range(0, 5)
//  println(l) // List(1, 2, 3)
  println(l2) // List(0, 1, 2, 3, 4)

  // List is immutable
  val one = List(1, 2)
  val two = List(3, 4)
  val three = one ::: two
  //    println(one + " and " + two + " were not mutated ")
  println("Thus, " + three + " is a new list")

  // Cons(::) prepends a new element to the beginning of an existing list and returns the resulting list
  // this is corresponding to three.::(0)
  val three2 = 0 :: three
    println(three2)

  // The reason you need Nil at the end is that :: is defined on class List. If you try to just
  //say 1 :: 2 :: 3, it won’t compile because 3 is an Int, which doesn’t have a :: method
  val nums = 1 :: 2 :: 3 :: Nil
  //  println(nums) // List(1, 2, 3)
}
